package com.example.springboot.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.resp.Result;
import com.example.springboot.common.utils.JwtToken;
import com.example.springboot.system.entity.ModelEntity;
import com.example.springboot.system.mapper.ModelMapper;
import com.example.springboot.system.service.ModelService;
import io.swagger.v3.oas.annotations.Operation;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * <p>
 * 模型表 前端控制器
 * </p>
 *
 * @author jiang
 * @since 2025-04-17
 */
@RestController
@RequestMapping("/modelEntity")
public class ModelController {

    private final ModelService service;
    private final ModelMapper mapper;

    public ModelController(ModelService service, ModelMapper mapper) {
        this.service = service;
        this.mapper = mapper;
    }

    @JwtToken
    @Operation(summary = "列表")
    @GetMapping("/modelList")
    public Result<?> modelList() {
        return Result.success(service.list());
    }

    @JwtToken
    @Operation(summary = "获取")
    @PostMapping("/modelId")
    public Result<?> modelId(@RequestParam Integer id) {
        return Result.success(mapper.selectById(id));
    }

    @JwtToken
    @Operation(summary = "列表分页")
    @PostMapping("/modelListPage")
    public Result<?> modelListPage(@RequestParam Integer page, @RequestParam Integer pageSize) {
        Page<ModelEntity> rowPage = new Page<>(page, pageSize);
        QueryWrapper<ModelEntity> wrapper = new QueryWrapper<>();
        rowPage = mapper.selectPage(rowPage, wrapper);
        return Result.success(rowPage);
    }

    @Data
    static class Model {
        private String name;
        private String content;
        private Date current;
        private String avatar;
    }

    @JwtToken
    @Operation(summary = "新增")
    @PostMapping("/modelAppend")
    public Result<?> modelAppend(@RequestBody ModelController.Model model) {
        ModelEntity entity = new ModelEntity();
        BeanUtils.copyProperties(model, entity);
        return Result.success(service.save(entity));
    }

    @JwtToken
    @Operation(summary = "删除")
    @PostMapping("/modelDelete")
    public Result<?> modelDelete(@RequestParam Integer id) {
        return Result.success(service.removeById(id));
    }

    @JwtToken
    @Operation(summary = "编辑")
    @PostMapping("/modelEditor")
    public Result<?> modelEditor(@RequestBody ModelEntity modelEntity) {
        QueryWrapper<ModelEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("id", modelEntity.getId());
        return Result.success(service.updateById(modelEntity));
    }

    @JwtToken
    @Operation(summary = "搜索")
    @PostMapping("/modelSearch")
    public Result<?> modelSearch(@RequestParam String name) {
        QueryWrapper<ModelEntity> wrapper = new QueryWrapper<>();
        wrapper.like("name", name);
        return Result.success(mapper.selectList(wrapper));
    }

}
